package com.yc.happ.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.happ.entity.Elder;
import com.yc.happ.entity.OrderItem;
import com.yc.happ.entity.OrderList;
import com.yc.happ.entity.Volunteer;
import com.yc.happ.service.BackService;
import com.yc.happ.util.DBHelper;
import com.yc.happ.util.HappConstant;

public class BackServiceImpl implements BackService{

	@Override
	public List<Volunteer> getAllVolunteer(Integer nowpage) {
		//开始页号
		Integer rownumStart=HappConstant.PAGESIZE*(nowpage-1);
		//结束页号
		Integer rownumEnd=HappConstant.PAGESIZE*nowpage;
		
		String sql=" select * from volunteer limit "+rownumStart+","+rownumEnd;;
		/*List<Object> params=new ArrayList<Object>();
		params.add(0,rownumStart);
		params.add(1, rownumEnd);
		*/List<Map<String,String>> listmap=DBHelper.doSelect(sql, null);
		List<Volunteer> listvol=new ArrayList<Volunteer>();
		
		for(Map<String,String> map:listmap){
			Volunteer vol=new Volunteer();
			vol.setVid(Integer.parseInt(map.get("vid")));
			vol.setVname(map.get("vname"));
			vol.setVuname(map.get("vuname"));
			vol.setVpassword(map.get("vpassword"));
			vol.setVsex(map.get("vsex"));
			vol.setVtype(map.get("vtype"));
			vol.setVaddress(map.get("vaddress"));
			vol.setVphone(map.get("vphone"));
			vol.setVspecial(map.get("vspecial"));
			vol.setVdate(map.get("vdate"));
			vol.setVtimeStart(map.get("vtimeStart"));
			vol.setVtimeEnd(map.get("vtimeEnd"));
			vol.setVscore(Integer.parseInt(map.get("vscore")));
			vol.setIsChoose(Integer.parseInt(map.get("isChoose")));
			listvol.add(vol);
		}
		return listvol;
	}

	@Override
	public List<Elder> getAllElder(Integer nowpage) {
		//开始页号
		Integer rownumStart=HappConstant.PAGESIZE*(nowpage-1);
		//结束页号
		Integer rownumEnd=HappConstant.PAGESIZE*nowpage;
		
		String sql=" select * from elder limit "+rownumStart+","+rownumEnd;;
		/*List<Object> params=new ArrayList<Object>();
		params.add(0,rownumStart);
		params.add(1, rownumEnd);*/
		List<Map<String,String>> listmap=DBHelper.doSelect(sql, null);
		List<Elder> listeld=new ArrayList<Elder>();
		
		for(Map<String,String> map:listmap){
			Elder eld=new Elder();
			eld.setVid(Integer.parseInt(map.get("vid")));
			eld.setVname(map.get("vname"));
			eld.setVuname(map.get("vuname"));
			eld.setVpassword(map.get("vpassword"));
			eld.setVsex(map.get("vsex"));
			eld.setVaddress(map.get("vaddress"));
			eld.setVaddress3(map.get("vaddress3"));
			eld.setVphone(map.get("vphone"));
			eld.setIsChoose(Integer.parseInt(map.get("isChoose")));
			listeld.add(eld);
		}
		return listeld;
	}

	@Override
	public List<OrderList> getAllOrderList(Integer nowpage) {
		//开始页号
		Integer rownumStart=HappConstant.PAGESIZE*(nowpage-1);
		//结束页号
		Integer rownumEnd=HappConstant.PAGESIZE*nowpage;
		
		String sql="(select o.oid,o.otime,e.vid as eid,e.vuname as euname,e.vphone as ephone,e.vaddress3 as eaddress,v.vid,v.vname,v.vphone,v.vaddress,v.vdate,v.vtimeStart,v.vtimeEnd,o.isFlag "
					+" from orderList o"
					+" left join elder e"
					+" on e.vid=o.eid"
					+" left join volunteer v"
					+" on o.vid=v.vid)"
					+" limit "+rownumStart+","+rownumEnd;
/*		List<Object> params=new ArrayList<Object>();
		params.add(0,rownumStart);
		params.add(1,rownumEnd);*/
		List<Map<String,String>> listmap=DBHelper.doSelect(sql,null);
		List<OrderList> listodl=new ArrayList<OrderList>();
		
		for(Map<String,String> map:listmap){
			OrderList odl=new OrderList();
			odl.setOid(Integer.parseInt(map.get("oid")));
			odl.setOtime(map.get("otime").substring(0,10));
			odl.setEid(Integer.parseInt(map.get("eid")));
			odl.setEuname(map.get("euname"));
			odl.setEphone(map.get("ephone"));
			odl.setEaddress(map.get("eaddress"));
			odl.setVid(Integer.parseInt(map.get("vid")));
			odl.setVname(map.get("vname"));
			odl.setVphone(map.get("vphone"));
			odl.setVaddress(map.get("vaddress"));
			odl.setVdate((map.get("vdate")));
			odl.setVtimeStart(map.get("vtimeStart"));
			odl.setVtimeEnd(map.get("vtimeEnd"));
			odl.setIsFlag(Integer.parseInt(map.get("isFlag")));
			listodl.add(odl);
		}
		return listodl;
	}

	@Override
	public List<OrderItem> getAllOrderItem(Integer nowpage) {
		
		//开始页号
		Integer rownumStart=HappConstant.PAGESIZE*(nowpage-1);
		//结束页号
		Integer rownumEnd=HappConstant.PAGESIZE*nowpage;
		
		String sql="select o.aid,o.vid,o.eid,o.aduring,o.aatitude,o.aquality,o.ascore,o.adate,o.isFlag,v.vname,v.vtype,e.vuname as euname"
				+" from orderItem o"
				+" left join volunteer v"
				+" on v.vid=o.vid"
				+" left join elder e"
				+" on e.vid=o.eid";
		List<Map<String,String>> listmap=DBHelper.doSelect(sql, null);
		List<OrderItem> listodt=new ArrayList<OrderItem>();
		
		for(Map<String,String> map:listmap){
			OrderItem odt=new OrderItem();
			odt.setAid(Integer.parseInt(map.get("aid")));
			odt.setVid(Integer.parseInt(map.get("vid")));
			odt.setEid(Integer.parseInt(map.get("eid")));
			odt.setVname(map.get("vname"));
			odt.setVtype(map.get("vtype"));
			odt.setEuname(map.get("euname"));
			odt.setAduring(Integer.parseInt(map.get("aduring")));
			odt.setAatitude(map.get("aatitude"));
			odt.setAquality(map.get("aquality"));
			odt.setAscore(Integer.parseInt(map.get("ascore")));
			odt.setAdate(map.get("adate"));
			odt.setIsFlag(Integer.parseInt(map.get("isFlag")));
			
			listodt.add(odt);
		}
		return listodt;
	}
	
}
